package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.image.ImageLoader;
import com.ksy.common.model.City;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.LvXinHttp;
import com.tech.connect.api.UserInfo;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemLvXinYao;
import com.tech.connect.model.PagerModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhaoFuWuLvYouYaoFragment extends BaseFragment {

    private BaseAdapter<ItemLvXinYao, BaseHolder> adapter;
    private RecyclerView recycler;
    private ArrayList<ItemLvXinYao> allList = new ArrayList<>();
    private int height;
    private ProxyLayout<RecyclerView> proxyLayout;
    private int pageSize = 15;
    private int page = 1;
    protected Map<String, Object> optMap = new HashMap<>();
    private City selectCity;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_zhaofuwu_lvyou_yao, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            Serializable citySer = arguments.getSerializable("city");
            if (citySer != null && (citySer instanceof City)) {
                selectCity = (City) citySer;
            }
        }
        initView(view);
        loadData();
    }


    private void initView(View view) {
        view.findViewById(R.id.tvPublish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(PublishLvYouYaoActivity.class, 996);
            }
        });
        int screenWidth = AndroidUtil.getScreenWidth(activity);
        height = (screenWidth - AndroidUtil.dip2px(50)) / 2;
        recycler = view.findViewById(R.id.recycler);
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new HLineDivider());
    }


    private void initAdapter(List<ItemLvXinYao> lvXinList) {
        if (lvXinList != null) {
            allList.addAll(lvXinList);
        }
        initAdapter();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<ItemLvXinYao, BaseHolder>(R.layout.item_layout_lvxing_yao, allList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemLvXinYao item = allList.get(position);
                    ImageView ivHead = helper.getView(R.id.ivHead);
                    TextView tvName = helper.getView(R.id.tvName);
                    ImageView ivSex = helper.getView(R.id.ivSex);
                    TextView tvAge = helper.getView(R.id.tvAge);
                    TextView tvDate = helper.getView(R.id.tvDate);
                    View vMessage = helper.getView(R.id.vMessage);
                    TextView tvCity = helper.getView(R.id.tvCity);
                    TextView tvBetweenDate = helper.getView(R.id.tvBetweenDate);
                    TextView tvWho = helper.getView(R.id.tvWho);
                    TextView tvHow = helper.getView(R.id.tvHow);
                    TextView tvStartDate = helper.getView(R.id.tvStartDate);
                    TextView tvMoney = helper.getView(R.id.tvMoney);
                    UserInfo user = item.user;
                    if (user != null) {
                        ImageLoader.getInstance().loadHead(activity, user.headImage, ivHead);
                        tvName.setText(user.nickName);
                        int age = user.age;
                        if (user.gender == 1) {
                            ivSex.setImageResource(R.drawable.icon_man);
                        } else {
                            ivSex.setImageResource(R.drawable.icon_women);
                        }
                        tvAge.setText(String.valueOf(age));
                    } else {
                    }
                    tvDate.setText(item.createTime);
                    tvCity.setText(item.cityName);
                    tvBetweenDate.setText(item.longTimeName);
                    tvWho.setText(item.inviteObjectName);
                    tvHow.setText(item.travelModeName);
                    tvStartDate.setText(item.departTimeName);
                    tvMoney.setText(item.travelFeeName);
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
//                    Intent intent = new Intent(activity, ZhaoFuWuLvYouViewActivity.class);
//                    intent.putExtra("dataList", allList);
//                    intent.putExtra("index", position);
//                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        if (selectCity != null) {
            map.put("cityCode", selectCity.id);
        }
        map.putAll(optMap);
        map.put("lat", CurrentInfo.getUserInfo().lat);
        map.put("lng", CurrentInfo.getUserInfo().lng);
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        LvXinHttp.lvxingYao(map, new BaseEntityOb<PagerModel<ItemLvXinYao>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemLvXinYao> data, String msg) {
                List<ItemLvXinYao> lvXinList = null;
                if (isSuccess && data != null) {
                    lvXinList = data.content;
                }
                if (page == 1) {
                    allList.clear();
                }
                initAdapter(lvXinList);
                if (lvXinList != null && lvXinList.size() >= pageSize) {
                    proxyLayout.setCanLoadMore(true);
                } else {
                    proxyLayout.setCanLoadMore(false);
                }
                if (allList.isEmpty()) {
                    proxyLayout.showEmptyView();
                } else {
                    proxyLayout.showContentView();
                }
                proxyLayout.dragFinish();
            }
        });
    }

    public void onOptClick(Map<String, Object> map) {
        //条件切换
        optMap.clear();
        optMap.putAll(map);
        loadData();

    }

    public void onCityClick(City city) {
        selectCity = city;
        loadData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (Activity.RESULT_OK == resultCode) {
            if (requestCode == 996) {
                page = 1;
                loadData();
            }
        }
    }
}